package com.greateamd.timetableasy.client.mvc.view.crud.planning;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.greateamd.timetableasy.client.mvc.model.PlanningModel;
import com.greateamd.timetableasy.client.mvc.view.TimeTableasyPage;
import com.greateamd.timetableasy.client.services.promotion.PromotionService;
import com.greateamd.timetableasy.client.services.promotion.PromotionServiceAsync;
import com.greateamd.timetableasy.client.services.util.ServerCall;
import com.greateamd.timetableasy.client.widgets.ClassicLargeLabel;
import com.greateamd.timetableasy.client.widgets.campus.PromotionListBox;
import com.greateamd.timetableasy.domain.Event;

public class PromotionPlanningPage <M extends PlanningModel> extends PlanningPage<M> {
	
	private final PromotionListBox listOfPromotionsAvailable;
	
	public PromotionPlanningPage(M model) {
		super(model);
		listOfPromotionsAvailable = new PromotionListBox(true);
		
		final FlowPanel panel = new FlowPanel();
		panel.add(new ClassicLargeLabel("Choose desired promotion"));
		panel.add(listOfPromotionsAvailable);
		setExtraPanel(panel);
	}

	//---------------------------------------------------------------------
	//-------------------------- Server call
	
	@Override
	protected void callServerEvent() {
		new ServerCall<List<Event>>() {

			@Override
			protected void callService(AsyncCallback<List<Event>> asyncCallback) {
				final PromotionServiceAsync promotionService = GWT.create(PromotionService.class);
				promotionService.getPlanning(listOfPromotionsAvailable.getSelectedPromotion(),getFromDate(),getToDate(),asyncCallback);
			}

			@Override
			public void onSuccess(List<Event> result) {
				model.setEvents(result);
				refreshPlanning(TimeTableasyPage.PLANNING_PROMOTION);
			}
		}.call();
	}

	@Override
	protected Widget getWidgetPage() {
		return this;
	}
}
